import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LXW
 * Date: 2022-04-08
 * Time: 21:25
 */
class MyBlockingQueue {
    private int[] elem = new int[10];
    private int head;
    private int tail;
    private int size;
    private Object locker = new Object();
    public void put(int val) {
        synchronized (locker) {
            if(size == elem.length) {
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            elem[tail] = val;
            locker.notify();
            tail++;
            if(tail == elem.length) {
                tail = 0;
            }
            size++;
        }
    }
    public int take() {
        synchronized (locker) {
            if(size == 0) {
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            int ret = elem[head];
            head++;
            if(head == elem.length) {
                head = 0;
            }
            size--;
            return ret;
        }
    }
}
public class Demo2 {
    public static void main(String[] args) {
        MyBlockingQueue queue = new MyBlockingQueue();
        Thread producer = new Thread(() -> {
            Random random = new Random();
           queue.put(random.nextInt(100));
        });
        Thread customer  = new Thread(() -> {
            int ret = queue.take();
        });
    }

}
